package cn.changein.framework.modular.gen.service;

import cn.changein.framework.base.database.entity.DatabaseInfo;
import cn.changein.framework.base.database.model.TableFieldInfo;
import cn.changein.framework.base.database.util.DbUtil;
import cn.changein.framework.kernel.validator.stereotype.ParamValidator;
import cn.changein.framework.modular.db.mapper.DatabaseInfoMapper;
import cn.changein.framework.modular.gen.enumeration.GenSessionKeyFlags;
import cn.changein.framework.modular.gen.model.FieldConfig;
import cn.changein.framework.modular.gen.model.GenSessionFieldConfigs;
import cn.changein.framework.modular.gen.model.param.SaveFieldConfigParam;
import cn.changein.framework.util.HttpContext;
import cn.changein.framework.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代码生成相关逻辑
 * 

 * @date: 2020-02-17
 * @copyright: 广州澄境科技有限公司
 */
@Service
public class GenerateService {

    @Autowired
    private DatabaseInfoMapper databaseInfoMapper;

    /**
     * 获取某个db容器下，某个表的字段配置
     * <p>
     * 如果之前设置过，则从缓存中拿出
     *
     * @param  dbId
     * @param tableName
     */
    public List<FieldConfig> getTableFieldsConfig(Long dbId, String tableName) {

        //查询session中有无已配置的字段数据，有配置过的直接返回
        HttpSession session = HttpContext.getRequest().getSession();
        GenSessionFieldConfigs sessionFieldConfigs = (GenSessionFieldConfigs) session.getAttribute(GenSessionKeyFlags.TABLE_FIELD_STYLES.name());
        if (sessionFieldConfigs != null && sessionFieldConfigs.containConfigFlag(tableName)) {
            return sessionFieldConfigs.getFieldConfigs(tableName);
        }

        //查找数据库元数据信息
        DatabaseInfo databaseInfo = databaseInfoMapper.selectById(dbId);

        //获取对应表的所有字段
        List<TableFieldInfo> tableFields = DbUtil.getTableFields(databaseInfo, tableName);

        //将表的所有字段信息转化为配置信息
        return tableFields.stream().map(i -> {
            FieldConfig fieldConfig = new FieldConfig();
            ToolUtil.copyProperties(i, fieldConfig);
            return fieldConfig;
        }).collect(Collectors.toList());
    }

    /**
     * 设置表的字段配置
     *
     * @param saveFieldConfigParam
     */
    @ParamValidator
    public void setTableFieldsConfig(SaveFieldConfigParam saveFieldConfigParam) {
        //从session获取配置
        HttpSession session = HttpContext.getRequest().getSession();
        GenSessionFieldConfigs sessionFieldConfigs = (GenSessionFieldConfigs) session.getAttribute(GenSessionKeyFlags.TABLE_FIELD_STYLES.name());
        if (sessionFieldConfigs == null) {
            sessionFieldConfigs = new GenSessionFieldConfigs();
        }

        //放入session配置树形
        sessionFieldConfigs.getFieldConfigs().put(saveFieldConfigParam.getTableName(),saveFieldConfigParam.getFieldConfigList());

        session.setAttribute(GenSessionKeyFlags.TABLE_FIELD_STYLES.name(), sessionFieldConfigs);
    }

}

